<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的扩展插件开发指南" tabindex="-1"><a class="header-anchor" href="#laravel-的扩展插件开发指南"><span>Laravel 的扩展插件开发指南</span></a></h2>
<ul>
<li><a href="#introduction">简介</a>
<ul>
<li><a href="#a-note-on-facades">Facade 注解</a></li>
</ul>
</li>
<li><a href="#package-discovery">扩展包发现</a></li>
<li><a href="#service-providers">服务提供者</a></li>
<li><a href="#resources">资源文件</a>
<ul>
<li><a href="#configuration">配置</a></li>
<li><a href="#migrations">数据库迁移</a></li>
<li><a href="#routes">路由</a></li>
<li><a href="#translations">语言包</a></li>
<li><a href="#views">视图</a></li>
</ul>
</li>
<li><a href="#commands">命令</a></li>
<li><a href="#public-assets">公用 Assets</a></li>
<li><a href="#publishing-file-groups">发布群组文件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>扩展包是添加功能到 Laravel 的主要方式。扩展包可以包含许多好用的功能，像 <a href="https://github.com/briannesbitt/Carbon" target="_blank" rel="noopener noreferrer">Carbon</a> 可用于处理时间，或像 <a href="https://github.com/Behat/Behat" target="_blank" rel="noopener noreferrer">Behat</a> 这种完整的 BDD 测试框架。</p>
<p>当然，这有非常多不同类型的扩展包。有些扩展包是独立运作的，意思是指他们并不依赖于任何 PHP 框架。刚刚所提到的 Carbon 及 Behat 就是这种扩展包。要在 Laravel 中使用这种扩展包只需要在 <code v-pre>composer.json</code> 文件里引入它们即可。</p>
<p>另一方面，有些扩展包特别指定只能在 Laravel 里面集成。这些扩展包可能包含路由、控制器、视图以及扩展包的相关设置，目的是增强 Laravel 本身的功能。这份指南里将主要以开发 Laravel 专属的扩展包为目标进行说明。</p>
<h3 id="facade-注解" tabindex="-1"><a class="header-anchor" href="#facade-注解"><span>Facade 注解</span></a></h3>
<p>当开发 Laravel 应用程序时，通常来讲你使用契约(contracts) 还是 facades 并没有什么区别，因为他们都提供了基本相同的水平的可测试性。但是，在进行扩展包开发的时候，你开发的扩展包并不能访问所有 Laravel 提供的测试辅助函数。如果想要别人在一个 Laravel 应用程序中，能够开发你。如果你想写扩展包的测试，并让这些测试看起来像是在一个典型的 Laravel 应用程序里，您可以使用 <a href="https://github.com/orchestral/testbench" target="_blank" rel="noopener noreferrer">Orchestral Testbench</a>。</p>
<h2 id="扩展包发现" tabindex="-1"><a class="header-anchor" href="#扩展包发现"><span>扩展包发现</span></a></h2>
<p>在 Laravel 应用程序的 <code v-pre>config/app.php</code> 配置文件中，<code v-pre>providers</code> 选项定义了应该被 Laravel 加载的服务提供者的列表。当有人安装你的扩展包时，你需要让你的服务提供者包含在这个列表里。而不是要求用户手动将你的服务提供者添加到这个列表里，你可能需要在你扩展包 <code v-pre>composer.json</code> 文件的 <code v-pre>extra</code> 部分的定义这些提供者。除了服务提供者，你也要列出可能想要注册的 <a href="https://learnku.com/docs/laravel/5.5/facades" target="_blank" rel="noopener noreferrer">facades</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string double-quoted-string">"extra"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"laravel"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"providers"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string double-quoted-string">"Barryvdh\\Debugbar\\ServiceProvider"</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string double-quoted-string">"aliases"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token string double-quoted-string">"Debugbar"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"Barryvdh\\Debugbar\\Facade"</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当 Laravel 安装的时候，一旦发现你的扩展包被配置，Laravel 将会自动的注册它的服务提供者和 facades，为扩展包的用户提供一个方便的安装体验。</p>
<h3 id="选择扩展包发现" tabindex="-1"><a class="header-anchor" href="#选择扩展包发现"><span>选择扩展包发现</span></a></h3>
<p>如果你是扩展包的使用者，你想要禁用一个包的扩展包发现，你可以在应用程序的 <code v-pre>composer.json</code> 文件的 <code v-pre>extra</code> 部分列出这个扩展包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string double-quoted-string">"extra"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"laravel"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"dont-discover"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string double-quoted-string">"barryvdh/laravel-debugbar"</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过在应用程序的 <code v-pre>dont-discover</code> 指令中使用 <code v-pre>*</code> 字符，禁用扩展包发现功能：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string double-quoted-string">"extra"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"laravel"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"dont-discover"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string double-quoted-string">"*"</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="服务提供者" tabindex="-1"><a class="header-anchor" href="#服务提供者"><span>服务提供者</span></a></h2>
<p><a href="https://learnku.com/docs/laravel/5.5/providers" target="_blank" rel="noopener noreferrer">服务提供者</a> 是你的扩展包与 Laravel 连接的重点。服务提供者负责绑定一些东西至 Laravel 的 <a href="https://learnku.com/docs/laravel/5.5/container" target="_blank" rel="noopener noreferrer">服务容器</a> 并告知 Laravel 要从哪加载扩展包的资源，例如视图、配置文件、语言包。</p>
<p>服务提供者继承了 <code v-pre>Illuminate\Support\ServiceProvider</code> 类并包含了两个方法： <code v-pre>register</code> 和 <code v-pre>boot</code>. 。基底的 <code v-pre>ServiceProvider</code> 类被放置在 Composer 的 <code v-pre>illuminate/support</code> 扩展包，你必须将它加入至你自己的扩展包依赖中。若要了解更多关于服务提供者的结构与用途，请查阅 <a href="https://learnku.com/docs/laravel/5.5/providers" target="_blank" rel="noopener noreferrer">它的文档</a>。</p>
<h2 id="资源文件" tabindex="-1"><a class="header-anchor" href="#资源文件"><span>资源文件</span></a></h2>
<h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>有时候，你可能想要将扩展包的配置文件发布到应用程序本身的 <code v-pre>config</code> 目录上。这能够让扩展包的用户轻松的重写这些默认的设置选项。如果要发布扩展包的配置文件，只需要在服务提供者里的 <code v-pre>boot</code> 方法内使用 <code v-pre>publishes</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后进行服务的启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/config/courier.php'</span> <span class="token operator">=></span> <span class="token function">config_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'courier.php'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在当扩展包的用户使用 Laravel 的 <code v-pre>vendor:publish</code> 命令时，扩展包的文件将会被复制到指定的位置上。当然，只要你的配置文件被发布，就可以如其它配置文件一样被访问：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'courier.option'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>{note} 您不应在配置文件中定义闭包函数。 当用户执行 <code v-pre>config:cache</code> Artisan命令时，它们不能正确地序列化。</p>
</blockquote>
<h4 id="默认的扩展包配置文件" tabindex="-1"><a class="header-anchor" href="#默认的扩展包配置文件"><span>默认的扩展包配置文件</span></a></h4>
<p>你也可以选择合并你的扩展包配置文件和应用程序里的副本配置文件。这样能够让你的用户在已经发布的副本配置文件中只包含他们想要重写的设置选项。如果想要合并配置文件，可在服务提供者里的 <code v-pre>register</code> 方法里使用 <code v-pre>mergeConfigFrom</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在容器中注册绑定。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">mergeConfigFrom</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/config/courier.php'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{note} 此方法仅合并配置数组的第一级。如果您的用户部分定义了多维配置数组，则不会合并缺失的选项。</p>
</blockquote>
<h3 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h3>
<p>如果您的扩展包中包含路由，您可以使用 <code v-pre>loadRoutesFrom</code> 方法加载它们。此方法将自动确定应用程序的路由是否已缓存，如果路由已缓存，将不会加载路由文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 执行服务的注册后启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadRoutesFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/routes.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="数据库迁移" tabindex="-1"><a class="header-anchor" href="#数据库迁移"><span>数据库迁移</span></a></h3>
<p>如果你的扩展包包含 <a href="https://learnku.com/docs/laravel/5.5/migrations" target="_blank" rel="noopener noreferrer">数据库迁移</a> ，你需要使用 <code v-pre>loadMigrationsFrom</code> 方法告知 Laravel 如何去加载他们。<code v-pre>loadMigrationsFrom</code> 方法只需要你的扩展包的迁移文件路径作为唯一参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后进行服务的启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadMigrationsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/migrations'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>完成扩展包迁移文件注册之后，在运行 <code v-pre>php artisan migrate</code> 命令时，它们就会自动被执行。你并不需要把他们导出到应用程序的 <code v-pre>database/migrations</code> 目录。</p>
<h3 id="语言包" tabindex="-1"><a class="header-anchor" href="#语言包"><span>语言包</span></a></h3>
<p>如果你的扩展包里面包含了 <a href="https://learnku.com/docs/laravel/5.5/localization" target="_blank" rel="noopener noreferrer">本地化</a> ，则可以使用 <code v-pre>loadTranslationsFrom</code> 方法来告知 Laravel 该如何加载它们。举个例子，如果你的扩展包名称为 <code v-pre>courier</code> ，你可以按照以下方式将其添加至服务提供者的 <code v-pre>boot</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后进行服务的启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadTranslationsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/translations'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>扩展包翻译参照使用了双冒号 <code v-pre>package::file.line</code> 语法。所以，你可以按照以下方式来加载 <code v-pre>courier</code> 扩展包中的 <code v-pre>messages</code> 文件 <code v-pre>welcome</code> 语句：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'courier::messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="发布语言包" tabindex="-1"><a class="header-anchor" href="#发布语言包"><span>发布语言包</span></a></h4>
<p>如果你想将扩展包的语言包发布至应用程序的 <code v-pre>resources/lang/vendor</code> 目录，则可以使用服务提供者的 <code v-pre>publishes</code> 方法。<code v-pre>publishes</code> 方法接受一个包含扩展包路径及对应发布位置的数组。例如，在 <code v-pre>courier</code> 扩展包中发布语言包，示例如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后进行服务的启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadTranslationsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/translations'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/translations'</span> <span class="token operator">=></span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'lang/vendor/courier'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在，当使用你扩展包的用户运行 Laravel 的 <code v-pre>vendor:publish</code> Artisan 命令时，扩展包的语言包将会被复制到指定的位置上。</p>
<h3 id="视图" tabindex="-1"><a class="header-anchor" href="#视图"><span>视图</span></a></h3>
<p>若要在 Laravel 中注册扩展包 <a href="https://learnku.com/docs/laravel/5.5/views" target="_blank" rel="noopener noreferrer">视图</a>，则必须告诉 Laravel 你的视图位置。你可以使用服务提供者的 <code v-pre>loadViewsFrom</code> 方法来实现。<code v-pre>loadViewsFrom</code> 方法允许两个参数：视图模板路径与扩展包名称。例如，如果你的扩展包名称是 <code v-pre>courier</code> ，你可以按照以下方式将其添加至服务提供者的 <code v-pre>boot</code> 方法内：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后进行服务的启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadViewsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/views'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>扩展包视图参照使用了双冒号 <code v-pre>package::view</code> 语法。所以，你可以通过如下方式从 <code v-pre>courier</code> 扩展包中加载 <code v-pre>admin</code> 视图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'courier::admin'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="重写扩展包视图" tabindex="-1"><a class="header-anchor" href="#重写扩展包视图"><span>重写扩展包视图</span></a></h4>
<p>当你使用 <code v-pre>loadViewsFrom</code> 方法时，Laravel 实际上为你的视图注册了 两个位置：一个是应用程序的 <code v-pre>resources/views/vendor</code> 目录，另一个是你所指定的目录。所以，以 <code v-pre>courier</code> 为例：当用户请求一个扩展包的视图时，Laravel 会在第一时间检查 <code v-pre>resources/views/vendor/courier</code> 是否有开发者提供的自定义版本视图存在。接着，如果这个路径没有自定义的视图，Laravel 会搜索你在扩展包 <code v-pre>loadViewsFrom</code> 方法里所指定的视图路径。这个方法可以让用户很方便的自定义或重写扩展包视图。</p>
<h4 id="发布视图" tabindex="-1"><a class="header-anchor" href="#发布视图"><span>发布视图</span></a></h4>
<p>若要发布扩展包的视图至 <code v-pre>resources/views/vendor</code> 目录，则必须使用服务提供者的 <code v-pre>publishes</code> 方法。<code v-pre>publishes</code> 方法允许一个包含扩展包视图路径及对应发布路径的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后进行服务的启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">loadViewsFrom</span><span class="token punctuation">(</span><span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/views'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'courier'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/views'</span> <span class="token operator">=></span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'views/vendor/courier'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在，当你的扩展包用户运行 Laravel 的 <code v-pre>vendor:publish</code> Artisan 命令时，扩展包的视图将会被复制到指定的位置上。</p>
<h2 id="命令" tabindex="-1"><a class="header-anchor" href="#命令"><span>命令</span></a></h2>
<p>给你的扩展包注册 Artisan 命令，您可以使用 <code v-pre>commands</code> 方法。此方法需要一个命令类的数组。一旦命令被注册，您可以使用 <a href="https://learnku.com/docs/laravel/5.5/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令行</a> 执行它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后进行服务的启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">runningInConsole</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">commands</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token class-name static-context">FooCommand</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">BarCommand</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="公用-assets" tabindex="-1"><a class="header-anchor" href="#公用-assets"><span>公用 Assets</span></a></h2>
<p>你的扩展包内可能会包含许多的资源文件，像 JavaScript、CSS 和图片等文件。如果要发布这些资源文件到应用程序的 <code v-pre>public</code> 目录上，只需使用服务提供者的 <code v-pre>publishes</code> 方法。在这个例子中，我们也会增加一个 <code v-pre>public</code> 的资源分类标签，可用于发布与分类关联的资源文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后进行服务的启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/path/to/assets'</span> <span class="token operator">=></span> <span class="token function">public_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vendor/courier'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在，当您的扩展包的用户执行 <code v-pre>vendor:publish</code> 命令时，您的 Assets 将被复制到指定的发布位置。由于每次更新包时通常都需要覆盖资源，因此您可以使用 <code v-pre>--force</code> 标志：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span><span class="token keyword">public</span> <span class="token operator">--</span>force</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="发布群组文件" tabindex="-1"><a class="header-anchor" href="#发布群组文件"><span>发布群组文件</span></a></h2>
<p>你可能想要分别发布分类的扩展包资源文件或是资源。举例来说，你可能想让用户不用发布扩展包的所有资源文件，只需要单独发布扩展包的配置文件即可。这可以通过在调用 <code v-pre>publishes</code> 方法时使用「标签」来实现。例如，让我们在扩展包的服务提供者中的 <code v-pre>boot</code> 方法定义两个发布群组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在注册后进行服务的启动。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/../config/package.php'</span> <span class="token operator">=></span> <span class="token function">config_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'package.php'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'config'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">publishes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/../database/migrations/'</span> <span class="token operator">=></span> <span class="token function">database_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'migrations'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'migrations'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>现在当你的用户使用 <code v-pre>vendor:publish</code> Artisan 命令时，就可以通过标签名称分别发布不同分类的资源文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>config</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/e421083458" target="_blank" rel="noopener noreferrer">@CraryPrimitiveMan</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/3020_1449383575.jpeg?imageView2/1/w/100/h/100" alt="100"></td>
<td>翻译</td>
<td><a href="https://github.com/CraryPrimitiveMan/create-your-own-php-framework" target="_blank" rel="noopener noreferrer">构建自己的PHP框架</a>，欢迎 Star。</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


